using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Body : Singleton<Body>
{
    public GameObject ha;

    private Transform  targetTransform;
    private float initialYSize;       
    private float targetScaleY = 1f;  
    private float scaleVelocity;      
    private float newHeight;
    
    
    [Header("Settings")]
    public float stretchSpeed = 1f;  
    public float smoothTime = 0.3f;

    private bool isSliding = false;
    

    public void Start()
    {
        targetTransform = GetComponent<Transform>();
        initialYSize = targetTransform.localPosition.y; ;
        targetScaleY = targetTransform.localScale.y;
    }
    
    public void Down()
    {
        // 持续计算新高度
        float currentHeight = initialYSize * targetScaleY;
        
        newHeight = currentHeight + stretchSpeed * Time.deltaTime;
        // 计算并更新目标缩放比例
        targetScaleY = newHeight / initialYSize;
        
        // 应用缩放
        ApplySmoothScale(targetScaleY);
    }
    public void StartReset()
    {
        PlayerController.Instance.ToggleSlide();
        
        targetScaleY = 1f;
        newHeight = initialYSize * targetScaleY;
        ApplySmoothScale(targetScaleY);
    }
    public void ApplySmoothScale(float targetscaley) {
        
        if ( targetScaleY != 1f)
        {
            float currentScaleY = Mathf.SmoothDamp(
                targetTransform.localScale.y,
                targetscaley,
                ref scaleVelocity,
                smoothTime
            );
            targetTransform.localScale = new Vector3(
                targetTransform.localScale.x,
                currentScaleY,
                targetTransform.localScale.z
            );
            ha.GetComponent<Transform>().localPosition = new Vector2(
                     ha.GetComponent<Transform>().localPosition.x,
                     -newHeight
            );
        }
        else
        {
            targetTransform.localScale = new Vector3(
                targetTransform.localScale.x,
                targetScaleY,
                targetTransform.localScale.z
            );
            ha.GetComponent<Transform>().localPosition = new Vector2(
                ha.GetComponent<Transform>().localPosition.x,
                -newHeight
            );
        }
        //LayoutRebuilder.ForceRebuildLayoutImmediate(targetTransform);
    }
}
